package cn.iocoder.yudao.module.member.service.interviewInvite;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.interviewInvite.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.interviewInvite.InterviewInviteDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.member.convert.interviewInvite.InterviewInviteConvert;
import cn.iocoder.yudao.module.member.dal.mysql.interviewInvite.InterviewInviteMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 面试邀约 Service 实现类
 *
 * @author 周枫皓
 */
@Service
@Validated
public class InterviewInviteServiceImpl implements InterviewInviteService {

    @Resource
    private InterviewInviteMapper interviewInviteMapper;
    @Resource
    private AdminUserApi adminUserApi;
    @Override
    public Long createInterviewInvite(InterviewInviteCreateReqVO createReqVO) {
        // 插入
        InterviewInviteDO interviewInvite = InterviewInviteConvert.INSTANCE.convert(createReqVO);
        AdminUserRespDTO adminUserRespDTO=adminUserApi.getUser(SecurityFrameworkUtils.getLoginUserId());
        interviewInvite.setPublisherId(adminUserRespDTO.getId());
        interviewInvite.setPublisherName(adminUserRespDTO.getNickname());
        interviewInvite.setTenantId(adminUserRespDTO.getTenantId());
        interviewInvite.setCreateTime(new Date());
        interviewInviteMapper.insert(interviewInvite);
        // 返回
        return interviewInvite.getId();
    }

    @Override
    public void updateInterviewInvite(InterviewInviteUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateInterviewInviteExists(updateReqVO.getId());
        // 更新
        InterviewInviteDO updateObj = InterviewInviteConvert.INSTANCE.convert(updateReqVO);
        updateObj.setUpdateTime(new Date());
        interviewInviteMapper.updateById(updateObj);
    }

    @Override
    public void deleteInterviewInvite(Long id) {
        // 校验存在
        this.validateInterviewInviteExists(id);
        // 删除
        interviewInviteMapper.deleteById(id);
    }
    @Override
    public void deleteInterviewInvites(List<Long> ids) {
        // 删除
        interviewInviteMapper.deleteBatchIds(ids);
    }

    private void validateInterviewInviteExists(Long id) {
        if (interviewInviteMapper.selectById(id) == null) {
            throw exception(INTERVIEW_INVITE_NOT_EXISTS);
        }
    }

    @Override
    public InterviewInviteDO getInterviewInvite(Long id) {
        return interviewInviteMapper.selectById(id);
    }

    @Override
    public List<InterviewInviteDO> getInterviewInviteList(Collection<Long> ids) {
        return interviewInviteMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InterviewInviteDO> getInterviewInvitePage(InterviewInvitePageReqVO pageReqVO) {
        return interviewInviteMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InterviewInviteDO> getInterviewInviteList(InterviewInviteExportReqVO exportReqVO) {
        return interviewInviteMapper.selectList(exportReqVO);
    }

    @Override
    public List<InterviewInviteDO> getInterviewInviteList(InterviewInviteReqVO reqVO) {
        return interviewInviteMapper.selectList(reqVO);
    }
    @Override
    public List<Long> getInterviewInviteIdList(InterviewInviteReqVO reqVO) {
        List<InterviewInviteDO> interviewInviteDOS=interviewInviteMapper.selectList(reqVO);
        List<Long> ids=new ArrayList<>();
        for (InterviewInviteDO interviewInviteDO:interviewInviteDOS
             ) {
            ids.add(interviewInviteDO.getId());
        }
        return ids;
    }

    @Override
    public Long resultCount(Long studentId,List<String> status) {
        return interviewInviteMapper.resultCount(studentId,status);
    }

    @Override
    public Long resultCount(Long studentId, String showStatus) {
        return interviewInviteMapper.resultCount(studentId,showStatus);
    }


}
